﻿using System;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;

namespace MyFramework.Helpers
{
    /// <summary>
    /// An helper class for Unity IoC framework.
    /// </summary>
    public static class UnityHelper
    {
        #region Fields

        /// <summary>
        /// The Unity container.
        /// </summary>
        private static IUnityContainer container = ServiceLocator.Current.GetInstance<IUnityContainer>();

        #endregion

        #region Resolve And Register Type Methods

        /// <summary>
        /// Registers the type of the and resolve.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="constructorParameters">The constructor parameters.</param>
        /// <returns></returns>
        public static T RegisterAndResolveType<T>(params object[] constructorParameters)
        {
            return (T)RegisterAndResolveType(typeof(T), constructorParameters);
        }

        /// <summary>
        /// Registers the type of the and resolve.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="name">The name.</param>
        /// <param name="constructorParameters">The constructor parameters.</param>
        /// <returns></returns>
        public static T RegisterAndResolveType<T>(string name, params object[] constructorParameters)
        {
            return (T)RegisterAndResolveType(typeof(T), name, constructorParameters);
        }

        /// <summary>
        /// Registers the type of the and resolve.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="constructorParameters">The constructor parameters.</param>
        /// <returns></returns>
        public static object RegisterAndResolveType(Type type, params object[] constructorParameters)
        {
            if (container.IsRegistered(type))
            {
                return container.Resolve(type);
            }
            else
            {
                object instance = Activator.CreateInstance(type, constructorParameters); //container.Resolve(type, overrides);
                container.RegisterInstance(type, instance);
                return instance;
            }

        }

        /// <summary>
        /// Registers the type of the and resolve.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="name">The name.</param>
        /// <param name="constructorParameters">The constructor parameters.</param>
        /// <returns></returns>
        public static object RegisterAndResolveType(Type type, string name, params object[] constructorParameters)
        {
            if (container.IsRegistered(type, name))
            {
                return container.Resolve(type, name);
            }
            else
            {
                object instance = Activator.CreateInstance(type, constructorParameters); //container.Resolve(type, overrides);
                container.RegisterInstance(type, name, instance);
                return instance;
            }
        }

        /// <summary>
        /// Registers the type of the and resolve.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="constructorParameters">The constructor parameters.</param>
        /// <returns></returns>
        public static object RegisterAndResolveType(string name, params object[] constructorParameters)
        {
            return RegisterAndResolveType(typeof(object), name, constructorParameters);
        }

        /// <summary>
        /// Registers the object.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="name">The name.</param>
        public static void RegisterObject(object obj, string name)
        {
            container.RegisterInstance(obj.GetType(), name, obj);
        }

        #endregion


    }
}
